package com.codetile.batch.db;

import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.xstream.XStreamMarshaller;

import java.nio.channels.ClosedSelectorException;
import java.util.HashMap;
import java.util.Map;

/**
 * 写入xml
 */

@Configuration
public class XmlItemWriter {

    @Bean
    public StaxEventItemWriter<User> xmlWriter() throws Exception {
        StaxEventItemWriter<User> writer=new StaxEventItemWriter<>();
        XStreamMarshaller marshaller=new XStreamMarshaller();
        Map<String, Class> map=new HashMap<>();
        map.put("user",User.class);
        marshaller.setAliases(map);
        writer.setRootTagName("users");
        writer.setMarshaller(marshaller);
        String path="f:\\user.xml";
        writer.setResource(new FileSystemResource(path));
        writer.afterPropertiesSet();
        return writer;
    }

}
